package com.coocaa.internship.exception;

import com.coocaa.internship.common.ResultCode;
import com.coocaa.internship.common.ResultData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartException;

/**
 * @Description: 全局异常捕获处理器
 * @Author: zwh
 * @Date: 2019-07-28
 * @Time: 12:54
 */
@Slf4j
@ControllerAdvice
public class ExceptionResolver {

    @ExceptionHandler(value = BaseException.class)
    @ResponseBody
    public ResultData baseExceptionHandler(BaseException e) {
        e.printStackTrace();
        return ResultData.builder().code(e.getCode()).message(e.getMessage()).build();
    }

    @ExceptionHandler(value = MultipartException.class)
    @ResponseBody
    public ResultData fileUploadExceptionHandler(MultipartException e) {
        e.printStackTrace();
        return ResultData.builder().code(ResultCode.FILE_NOT_EXISTS.getCode()).message(ResultCode.FILE_NOT_EXISTS.getMessage()).build();
    }

    @ExceptionHandler(value = MissingServletRequestParameterException.class)
    @ResponseBody
    public ResultData missingServletRequestParameterExceptionHandler(MissingServletRequestParameterException e) {
        e.printStackTrace();
        return ResultData.builder().code(ResultCode.PARAMETER_ERROR.getCode()).message("参数字段为空").build();
    }
}
